package cn.wolfcode.wolfcodemall.controller.admin;

import cn.wolfcode.wolfcodemall.domain.Category;
import cn.wolfcode.wolfcodemall.qo.CategoryQo;
import cn.wolfcode.wolfcodemall.qo.PageQo;
import cn.wolfcode.wolfcodemall.service.CategoryService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 分类相关请求的控制器
 */
@Controller
@RequestMapping("/admin")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    //分类管理页面请求
    @RequestMapping("/category")
    public String category(Model model, @ModelAttribute("qo") CategoryQo qo,String pids){
        //条件查询分类信息
        PageInfo<Category> pageResult = categoryService.findByCondition(qo);

        //分页查询分类信息
        //PageInfo<Category> pageResult = categoryService.findByPage(qo);
        model.addAttribute("pageResult",pageResult);

        model.addAttribute("pids",pids);

//        //查询所有的分类信息
//        List<Category> categoryList = categoryService.findAll();
//        //将分类信息携带到页面中显示
//        model.addAttribute("categoryList",categoryList);
        return "/admin/categoryManagePage";
    }

    //分类编辑页面
    @RequestMapping("/category/input")
    public String input(Model model){
        //在跳转页面前，需要携带propertys数据，由于目前做的是添加，没有属性信息，在model中存储propertys为no
        model.addAttribute("propertys","no");
        return "/admin/categoryinput";
    }

    //分类的保存或更新请求
    @RequestMapping("/category/saveOrUpdate")
    public String saveOrUpdate(Category category,String propertys){
        //将获取到的数据保存到数据库
        categoryService.saveOrUpdate(category,propertys);
        return "redirect:/admin/category";
    }

    //分类详情
    @RequestMapping("/category/input/{categoryId}")
    public String input(@PathVariable Integer categoryId,Model model){
        //根据分类id查询到对应的分类信息，将分类信息带到页面回显
        Category category = categoryService.findById(categoryId);

        //查询分类的属性信息
        List<String> propertys = categoryService.findPropertyNameByCategoryId(categoryId);
        //将集合转换为json格式字符串
        String jsonString = JSONObject.toJSONString(propertys);
        model.addAttribute("propertys",jsonString);

        model.addAttribute("category",category);

        return "/admin/categoryinput";
    }
    //删除分类请求
    @RequestMapping("/category/delete/{categoryId}")
    public String delete(@PathVariable Integer categoryId){
        //根据categoryId删除对应的分类信息
        try{
            categoryService.delete(categoryId);
        }catch(Exception e){
            //获取异常信息      商品的id
            String pids = e.getMessage();
            return "redirect:/admin/category?pids=" + pids;
        }

        //删除完毕后，跳转 回到列表页面
        return "redirect:/admin/category";
    }
}
